package com.hyz.login.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hyz.login.mapper.CashierMapper;
import com.hyz.login.model.Cashier;
import com.hyz.login.model.Pharmacy;
import com.hyz.login.model.Report;
import com.hyz.login.service.CashierService;
import com.hyz.login.service.PharmacyService;
import com.hyz.login.service.ReportService;
import com.hyz.login.util.PageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 2022/11/19  何昱泽
 */
@Controller
public class CcController {

    Logger logger = LoggerFactory.getLogger(CcController.class);

    @Autowired
    private ReportService reportService;

    @Autowired
    private PharmacyService pharmacyService;

    @Autowired
    private CashierService cashierService;

    @RequestMapping("/cc/findPage")
    @ResponseBody
    public String findPage(String durgname,String pharmacyName,
                           @RequestParam(required = false,defaultValue = "1")Integer page,
                           @RequestParam(required = false,defaultValue = "5")Integer limit){
        if (durgname==null && pharmacyName !=null){
            durgname=pharmacyName;
        }
        IPage<Report> iPage = reportService.findPage(durgname, page, limit);
        return "{\"msg\":\"\",\"code\":0,\"count\":"+iPage.getTotal()+",\"data\":"+ JSON.toJSONString(iPage.getRecords())+"}";
    }

    @RequestMapping("/page/rep/index")
    public String toRegister(Model model, String params, String cc){
        model.addAttribute("ccdefault",cc);
        model.addAttribute("reportList",reportService.findAll(params,cc));
        return "rep/toRegister";
    }

    @RequestMapping("/page/caocc/seldrug")
    @ResponseBody
    public String findPharmacy(@RequestParam(required = false,defaultValue = "")String durgname,
                                       @RequestParam(required = false,defaultValue = "1")Integer page,
                                       @RequestParam(required = false,defaultValue = "5")Integer limit){
        IPage<Pharmacy> iPage = pharmacyService.findAll(durgname, page, limit);
        return "{\"msg\":\"\",\"code\":0,\"count\":"+iPage.getTotal()+",\"data\":"+ JSON.toJSONString(iPage.getRecords())+"}";
    }


    @RequestMapping("/page/cc/selbing")
    @ResponseBody
    public String selbing(Integer reid){
        return reportService.findZhuan(reid);
    }

    @RequestMapping("/page/cc/addbing")
    @ResponseBody
    public Integer addbing(Integer reid,String bing){
        return reportService.modifyZhuanByReid(reid, bing);
    }

    @RequestMapping("/page/cc/selchu")
    @ResponseBody
    public Integer selchu(Integer reid,String mename){
        return cashierService.findCountByIdAndName(reid, mename);
    }


    @RequestMapping("/page/cc/selall")
    @ResponseBody
    public String selall(Integer perid,
                         @RequestParam(required = false,defaultValue = "1")Integer page,
                         @RequestParam(required = false,defaultValue = "5")Integer limit){
        IPage<Cashier> iPage = cashierService.findByReportId(perid, page, limit);
        return "{\"msg\":\"\",\"code\":0,\"count\":"+iPage.getTotal()+",\"data\":"+ JSON.toJSONString(iPage.getRecords())+"}";
    }


    @RequestMapping("/page/cc/addchu")
    @ResponseBody
    public Integer addchu(Cashier cashier,Integer drugstorenum){
        pharmacyService.modifyDrugstorenum(cashier.getDurgname(),drugstorenum);
        cashier.setState(0);
        return cashierService.addCashier(cashier);
    }

    @RequestMapping("/page/cc/updchu")
    @ResponseBody
    public Integer updchu(Cashier cashier,Integer drugstorenum){
        drugstorenum = drugstorenum-cashier.getDurgnum();
        pharmacyService.modifyDrugstorenum(cashier.getDurgname(),drugstorenum);
        return cashierService.updCashier(cashier);
    }

    @RequestMapping("/page/rep/del")
    @ResponseBody
    public String del(Integer cashier,String durnme,Integer durnum){
        pharmacyService.addDrugstorenum(durnme,durnum);
        if (cashierService.delById(cashier)>0){
            return "删除成功";
        }
        return "删除失败";
    }
}
